/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiclander.adhocracy.jira.cli;

import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.SearchRestClient;
import com.atlassian.jira.rest.client.domain.SearchResult;
import com.logiclander.adhocracy.cli.CommandLineReactor;
import static com.logiclander.adhocracy.jira.cli.JiraCommandLineParserFactory.ARG_PROJECTKEY;
import static com.logiclander.adhocracy.jira.cli.JiraCommandLineParserFactory.OPT_FILTERWATCHER;
import static com.logiclander.adhocracy.jira.cli.JiraCommandLineParserFactory.TGL_CUSTOMRANK;
import static com.logiclander.adhocracy.jira.cli.JiraCommandLineParserFactory.TGL_STATUSCLOSED;
import static com.logiclander.adhocracy.jira.cli.JiraCommandLineParserFactory.TGL_STATUSRESOLVED;
import static com.logiclander.adhocracy.jira.cli.JiraCommandLineParserFactory.TGL_TYPESUBTASK;
import static com.logiclander.adhocracy.jira.cli.JiraCommandLineParserFactory.TGL_WATCHING;
import org.apache.commons.cli2.CommandLine;
import org.springframework.context.ApplicationContext;

/**
 *
 * @author tim
 */
public class JiraListIssuesReactorFactory extends JiraCommandLineReactorFactory {

    public JiraListIssuesReactorFactory(ApplicationContext context) {
        super(context);
    }

    public CommandLineReactor createReactor(CommandLine cl) {
        final NullProgressMonitor pm = new NullProgressMonitor();
        final SearchRestClient searchClient = getJiraRestClient().getSearchClient();
        SearchResult sr;
        String searchQuery = "";
        if (cl.hasOption(ARG_PROJECTKEY)) {
            searchQuery = "project = " + (String) cl.getValue(ARG_PROJECTKEY);
        }
        if (cl.hasOption(OPT_FILTERWATCHER)) {
            if (!searchQuery.isEmpty()) {
                searchQuery = searchQuery + " AND";
            }
            searchQuery = searchQuery + " watcher = " + cl.getValue(OPT_FILTERWATCHER);
        }
        if (cl.getSwitch(TGL_WATCHING)) {
            if (!searchQuery.isEmpty()) {
                searchQuery = searchQuery + " AND";
            }
            searchQuery = searchQuery + " watcher = " + getJiraApiUsr();
        }
        if (!cl.getSwitch(TGL_TYPESUBTASK)) {
            if (!searchQuery.isEmpty()) {
                searchQuery = searchQuery + " AND";
            }
            searchQuery = searchQuery + " issuetype != Sub-task";
        }
        if (!cl.getSwitch(TGL_STATUSCLOSED)) {
            if (!searchQuery.isEmpty()) {
                searchQuery = searchQuery + " AND";
            }
            searchQuery = searchQuery + " status != Closed";
        }
        if (!cl.getSwitch(TGL_STATUSRESOLVED)) {
            if (!searchQuery.isEmpty()) {
                searchQuery = searchQuery + " AND";
            }
            searchQuery = searchQuery + " status != Resolved";
        }
        if (!cl.getSwitch(TGL_CUSTOMRANK)) {
            searchQuery = searchQuery + " ORDER BY status DESC, key DESC";
        } else {
            if (!searchQuery.isEmpty()) {
                searchQuery = searchQuery + " AND";
            }
            searchQuery = searchQuery + " cf[10320] != null";
            searchQuery = searchQuery + " ORDER BY cf[10320] ASC, status DESC, key DESC";
        }
        System.out.println("----------------------------------------------------------------------");
        System.out.println("-- Query: " + searchQuery);
        System.out.println("----------------------------------------------------------------------");
        sr = searchClient.searchJql(searchQuery, pm);
        return new JiraListIssuesReactor(sr, getJiraRestClient(), pm);
    }
}
